<template>
  <div class="home-container">
    <!-- 推荐博客 -->
    <section class="recommended-blogs">
      <h2>推荐博客</h2>
      <ul>
        <li v-for="blog in Blogs" :key="blog.bid" @click="navigateToDetail(blog.bid)">
          <h3>{{ blog.title }}</h3>
          <p class="blog-content">{{ blog.content }}</p>
          <div class="blog-meta">
            <span>作者: {{ blog.authorName }}</span>
            <span>点赞数: {{ blog.countLike }}</span>
            <span>评论数: {{ blog.Comments }}</span>
          </div>
          <div class="blog-tags" v-if="blog.label && blog.label.length > 0">
            <span class="tag" v-for="tag in blog.label" :key="tag">{{ tag }}</span>
          </div>
          <div class="divider"></div>
        </li>
      </ul>
    </section>
  </div>
</template>

<script>
import api from '@/api/index';
import {useRouter} from 'vue-router';

export default {
  name: 'Home',
  data() {
    return {
      Blogs: []
    };
  },
  setup() {
    const router = useRouter();
    return {router};
  },
  mounted() {
    this.fetchRecommendedBlogs();
    // console.log("全局用户信息==" , this.$store.state.user);
  },
  methods: {
    async fetchRecommendedBlogs() {
      try {
        const response = await api.get("/api/bLogList");
        console.log(response);
        this.Blogs = response.serverData;
        // 获取每篇博客的评论数 并设置
        for (let blog of this.Blogs) {
          api.get("/api/getComments", {params: {"bid": blog.bid}}).then(res => {
            console.log(res)
            console.log(res.serverData.length)
            blog.Comments = res.serverData.length;
          });

        }

      } catch (error) {
        console.error('获取推荐博客失败', error);
      }
    },
    navigateToDetail(blogId) {
      this.router.push(`/blog/${blogId}`);
    },
    // ,写一个get博客评论 数量的方法
    getComments(bid) {

      api.get("/api/getComments", {params: {"bid": bid}}).then(res => {
            console.log(res)
            console.log(res.serverData.length)
            return res.serverData.length;
          }
      );


    }


  }
};
</script>

<style scoped>
.author-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
}

.blog-title {
  font-weight: bold;
  cursor: pointer;
  text-align: left;
  display: block;
  margin-bottom: 10px;
}

.home-container {
  padding: 20px;
  max-width: 1200px; /* 增加最大宽度 */
  margin: 0 auto; /* 居中显示 */
}

section {
  margin-bottom: 40px;
}

h2 {
  font-size: 24px;
  margin-bottom: 20px;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f9f9f9;
  border-radius: 5px;
}

.blog-content {
  max-height: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.blog-meta {
  font-size: 14px;
  color: #666;
  margin-top: 10px;
}

.blog-meta span {
  margin-right: 15px;
}

.blog-tags {
  margin-top: 10px;
}

.tag {
  display: inline-block;
  background-color: #e1f5fe;
  color: #007bff;
  padding: 2px 8px;
  border-radius: 16px;
  font-size: 12px;
  margin-right: 5px;
  margin-bottom: 5px;
}

.divider {
  height: 1px;
  background-color: #ddd;
  margin: 15px 0;
}
</style>